package net.lz1998.pbbot.plugin;

import net.lz1998.pbbot.bot.Bot;
import net.lz1998.pbbot.bot.BotPlugin;
import net.lz1998.pbbot.constant.BannedConstant;
import net.lz1998.pbbot.constant.MinGanCi;
import net.lz1998.pbbot.constant.TitleConstant;
import net.lz1998.pbbot.utils.Msg;
import onebot.OnebotEvent;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 敏感词功能
 */
@Component
public class BannedPlugin extends BotPlugin {

    private static boolean flag=false;


    @Override
    public int onGroupMessage(@NotNull Bot bot, @NotNull OnebotEvent.GroupMessageEvent event) {
        String msg=event.getRawMessage().trim() ;
        long groupId = event.getGroupId();
        long id = event.getUserId();

        if(BannedConstant.ADMINID == id||BannedConstant.ADMINID1 == id) {
            if (BannedConstant.GROUPID == groupId && msg.startsWith(BannedConstant.MINGANCION)){
                flag = true;
                bot.sendGroupMsg(groupId, "已开启敏感词机制", true);
                bot.sendGroupMsg(groupId, Msg.builder().face(1), false);
            } else if (BannedConstant.GROUPID == groupId && msg.startsWith(BannedConstant.MINGANCIOFF)){
                flag = false;
                bot.sendGroupMsg(groupId, "已关闭敏感词机制", true);
                bot.sendGroupMsg(groupId, Msg.builder().face(0), false);
            }
        }

        //敏感词
        if(flag) {
            String[] split = MinGanCi.minganci.split(" ");
            for (int i = 0; i < split.length; i++) {
                if (msg.contains(split[i]) && BannedConstant.ADMINID != id) {
                    bot.sendGroupMsg(groupId, "用户[" + id + "]:" + msg + "含有敏感词汇:" + split[i], true);
                    bot.sendGroupMsg(groupId, "用户[" + id + "]:" + BannedConstant.MINGAN, true);
                    bot.setGroupBan(groupId, id, 2 * 60);
                    break;
                }
            }
            if (msg.length() > 0 && (msg.startsWith("?") || msg.startsWith("？")) && BannedConstant.ADMINID != id) {
                bot.sendGroupMsg(groupId, "用户[" + id + "]" + BannedConstant.MIAOSHI, true);
                bot.setGroupBan(groupId, id, 2 * 60);
            }
        }

        String[] arr = msg.split(" ");
        if(BannedConstant.GROUPID==groupId&&msg.startsWith(BannedConstant.HEARD)&&arr.length>1){
            if(arr.length==3) {
                if(arr[2].startsWith("qq=")){
                    String[] idArr = arr[2].split("=");
                    Long user_id = Long.valueOf(idArr[1].substring(1, idArr[1].lastIndexOf('"')));
                    Long adminId = event.getUserId();
                    if(user_id== TitleConstant.ADMINID){
                        bot.setGroupSpecialTitle(groupId, adminId, BannedConstant.CURRENTHEARD, 1);
                        bot.setGroupBan(groupId,adminId,5*60);
                    }else{
                        bot.setGroupBan(groupId,user_id,1*60);
                    }
                }else{
                    bot.sendGroupMsg(groupId, BannedConstant.INFO, true);
                }
            }
            if(arr.length==4){
                try {
                    if(arr[2].startsWith("qq=")){
                        String[] idArr = arr[2].split("=");
                        Long user_id = Long.valueOf(idArr[1].substring(1, idArr[1].lastIndexOf('"')));
                        Long adminId = event.getUserId();
                        String time = arr[3];
                        Integer times=0;
                        try {
                            times= Integer.parseInt(time);
                        } catch (NumberFormatException e) {
                            e.printStackTrace();
                            bot.sendGroupMsg(groupId, BannedConstant.INFO, true);
                        }

                        if(adminId==TitleConstant.ADMINID){
                            bot.setGroupBan(groupId,user_id,times*60);
                        }else{
                            bot.sendGroupMsg(groupId, BannedConstant.QUNZHUINFO, true);
                        }
                    }else{
                        bot.sendGroupMsg(groupId, BannedConstant.QUNZHUINFO, true);
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
        }else if(BannedConstant.GROUPID==groupId&&msg.startsWith(BannedConstant.UNHEARD)&&arr.length>1){
            if(arr.length==3){


//                bot.sendGroupMsg(groupId, "[bot:image,file=1.jpg]", false);
//                bot.sendDiscussMsg(groupId, "[bot:emoji,id=128513]", false);

                try {
                    if(arr[2].startsWith("qq=")){
                        String[] idArr = arr[2].split("=");
                        Long user_id = Long.valueOf(idArr[1].substring(1, idArr[1].lastIndexOf('"')));
                        Long adminId = event.getUserId();
                        bot.setGroupBan(groupId,user_id,0);
                    }else{
                        bot.setGroupSpecialTitle(groupId,event.getUserId(),TitleConstant.ZHUHEARD,1);
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
        }
        return MESSAGE_IGNORE;
    }


    public static String timestamp2Date(Long time){
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(time * 1000));
    }
}




